package com.itic.mall.system.orderproducts.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.Constants;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.UserSessionHelper;
import com.itic.mall.system.expendlog.persistence.dao.ExpendLogDao;
import com.itic.mall.system.expendlog.persistence.model.ExpendLog;
import com.itic.mall.system.order.persistence.dao.OrderDao;
import com.itic.mall.system.order.persistence.model.Order;
import com.itic.mall.system.orderexpress.persistence.dao.OrderExpressDao;
import com.itic.mall.system.orderexpress.persistence.model.OrderExpress;
import com.itic.mall.system.orderpayback.persistence.dao.OrderPaybackDao;
import com.itic.mall.system.orderpayback.persistence.model.OrderPayback;
import com.itic.mall.system.orderproducts.persistence.dao.OrderProductsDao;
import com.itic.mall.system.orderproducts.persistence.model.OrderProducts;
import com.itic.mall.system.orderproducts.persistence.model.OrderProductsExample;
import com.itic.mall.system.orderproducts.persistence.model.OrderProductsFeedbackVo;
import com.itic.mall.system.orderproducts.persistence.model.OrderProductsParam;
import com.itic.mall.system.orderproducts.persistence.model.OrderProductsVo;
import com.itic.mall.system.statis.persistence.dao.StatisDao;
import com.itic.mall.system.statis.persistence.model.Statis;


@Service
@Transactional
public class OrderProductsService
     extends CrudService<OrderProductsDao,OrderProducts,OrderProductsExample> {
    @Autowired
    private OrderProductsDao orderProductsDao;
    @Autowired
    private OrderPaybackDao orderPaybackDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ExpendLogDao expendLogDao;
    @Autowired
    private StatisDao statisDao;
    @Autowired
    private OrderExpressDao orderExpressDao;

	public List<OrderProductsVo> listByOrderId(String id) {
		return orderProductsDao.listByOrderId(id);
	}

	//查找没有评价的订单项
	public List<OrderProductsFeedbackVo> listNoFeedback(OrderProductsParam param) {
		return orderProductsDao.listNoFeedBack(param);
	}

	//根据条件查找订单项信息
	public List<OrderProductsFeedbackVo> listOrderProducts(OrderProductsParam param) {
		return orderProductsDao.listOrderProducts(param);
	}


	//查找没有评价的订单项
	public Integer countListNoFeedback(OrderProductsParam param) {
		return orderProductsDao.countListNoFeedback(param);
	}

	//根据条件查找订单项信息
	public Integer countListOrderProducts(OrderProductsParam param) {
		return orderProductsDao.countListOrderProducts(param);
	}

	public String payBack(OrderProducts item, OrderPayback payback,
			ExpendLog expendLog, Statis statis) {
		orderProductsDao.updateByPrimaryKeySelective(item);
		orderPaybackDao.insert(payback);
		expendLogDao.insert(expendLog);
		statisDao.insert(statis);
		return SUCCESS;
	}
	
	public String payBackNew(OrderProducts item, OrderPayback payback,Order order) {
		orderProductsDao.updateByPrimaryKeySelective(item);
		orderPaybackDao.insert(payback);
		orderDao.updateByPrimaryKeySelective(order);
		return SUCCESS;
	}

	public String giveback(OrderProducts item, OrderExpress express) {
		orderProductsDao.updateByPrimaryKeySelective(item);
		express.setId( IdGen.uuid() );
		express.setIsComeback("1");
		express.setIsEffective(DBConst.TRUE);
		express.setCreateTime(new Date());
		express.setUpdateTime(new Date());
		express.setOrgCode(null != UserSessionHelper.get(Constants.USER_ORG) ? UserSessionHelper
                .get(Constants.USER_ORG) : "unknown");
		express.setCreateUser(null != UserSessionHelper.get(Constants.USER_ID) ? UserSessionHelper
                .get(Constants.USER_ID) : "unknown");
		orderExpressDao.insert(express);
		return SUCCESS;
	}

}